home *** CD-ROM | disk | FTP | other *** search
- /*
- ImageStatistics.c
-
- ImageStatistics returns min, max, mean, and meanSquare values of the pixels in
- the supplied rect, after clipping with the GWorld's portRect. The supplied
- rect is replaced by the clipped rect.
-
- ImageEnergy returns the summed square value, over the clipped rect area, of
- the pixels after subtracting the supplied "background".
-
- HISTORY:
- 10/27/94 wrote it
- 8/15/95 dgp changed "0.0/0.0" to NAN to work around bug in Symantec C++ reported by Bosco.
- */
- #include "VideoToolbox.h"
-
- double ImageEnergy(GWorldPtr world,Rect *aRect,double background)
- {
- double mean,meanSquare,area;
-
- ImageStatistics(world,aRect,NULL,NULL,&mean,&meanSquare);
- area=(double)(aRect->right-aRect->left)*(aRect->bottom-aRect->top);
- return area*(meanSquare-2*background*mean+background*background);
- }
-
- void ImageStatistics(GWorldPtr world,Rect *rect
- ,long *minPtr,long *maxPtr,double *meanPtr,double *meanSquarePtr)
- {
- register long pix,pixMax,pixMin,meanL,meanSquareL;
- register double mean,meanSquare;
- double area;
- int width;
- register int i,j;
- int error=0;
- unsigned long pixels[1024];
-
- assert(StackSpace()>4000);
- SectRect(rect,&world->portRect,rect);
- if(EmptyRect(rect)){
- mean=meanSquare=NAN;
- pixMin=pixMax=0;
- }else{
- width=rect->right-rect->left;
- assert(width<=sizeof(pixels)/sizeof(*pixels));
- mean=meanSquare=0;
- pixMin=LONG_MAX;
- pixMax=LONG_MIN;
- for(j=rect->top;j<rect->bottom;j++){
- GetWindowPixelsQuickly((WindowPtr)world,rect->left,j,pixels,width);
- meanL=0;
- meanSquareL=0;
- for(i=width-1;i>=0;i--){
- pix=pixels[i];
- if(pix<pixMin)pixMin=pix;
- if(pix>pixMax)pixMax=pix;
- meanL+=pix;
- meanSquareL+=pix*pix;
- }
- mean+=meanL; // float
- meanSquare+=meanSquareL; // float
- }
- area=(double)width*(rect->bottom-rect->top);
- mean/=area;
- meanSquare/=area;
- }
- if(minPtr!=NULL)*minPtr=pixMin;
- if(maxPtr!=NULL)*maxPtr=pixMax;
- if(meanPtr!=NULL)*meanPtr=mean;
- if(meanSquarePtr!=NULL)*meanSquarePtr=meanSquare;
- }
-